"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
console.log('---------------------装饰器Decorator--------------------');
console.log('---------------------类装饰器ClassDecorator--------------------');
// 不破坏类的结构，给类增加属性或者方法
// 定义一个类装饰器
const Base = (target) => {
    //   console.log('target:', target) //类的构造函数
    // 给类增加属性或者方法
    target.prototype.treeAge = 100;
    target.prototype.getTree = () => {
        console.log('我是树');
    };
};
//写法一：
let Tree = class Tree {
    constructor(treeName) {
        this.treeName = treeName;
    }
    getTreeName() {
        return this.treeName;
    }
};
Tree = __decorate([
    Base,
    __metadata("design:paramtypes", [String])
], Tree);
// 写法二：Base(Tree)  用来兼容不支持@Base语法的浏览器
// Base(Tree)
let tree = new Tree('大树');
console.log(tree.treeName, tree.treeAge, tree.getTreeName(), tree.getTree());
console.log('---------------------类装饰器ClassDecorator--装饰器工厂（函数看柯里化）------------------');
// 装饰器工厂  用来传参  使用的是函数柯里化
const Base2 = (params) => {
    const fn = (target) => {
        target.prototype.tableName = params;
    };
    return fn;
};
let Table = class Table {
};
Table = __decorate([
    Base2('我是参数')
], Table);
let table = new Table();
console.log('tableName:', table.tableName);
console.log('---------------------方法装饰器MethodDecorator--装饰器工厂（函数看柯里化）----------------');
// 封装一个简单的GET请求
const Get = (url) => {
    const fn = (target, propertyKey, descriptor) => {
        // console.log('target:', target)
        // console.log('propertyKey:', propertyKey)
        // console.log('descriptor:', descriptor)
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.onreadystatechange = () => {
            if (xhr.status == 200 && xhr.readyState == 4) {
                let result = JSON.parse(xhr.response).result;
                target.list = result.list; //也可以类中直接增加属性list并赋值
                descriptor.value(result);
            }
        };
        xhr.send(null);
    };
    return fn;
};
const Name = (target, propertyKey) => {
    console.log('------属性装饰器PropertyDecorator-----');
    console.log('target:', target);
    console.log('---propertyKey:', propertyKey);
};
class Http {
    constructor() {
        this.name = '奇奇';
    }
    getList(data) {
        console.log('获取到的数据：', data);
    }
}
__decorate([
    Name,
    __metadata("design:type", String)
], Http.prototype, "name", void 0);
__decorate([
    Get('https://api.apiopen.top/api/getHaoKanVideo?page=0&size=10'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", void 0)
], Http.prototype, "getList", null);
let http = new Http();
const debounce = (delay) => {
    let timeout;
    const fn = (target, propertyKey, descriptor) => {
        const value = descriptor.value;
        descriptor.value = function (...args) {
            if (timeout)
                clearTimeout(timeout);
            timeout = setTimeout(() => {
                timeout = null;
                value.apply(this, args);
            }, delay);
        };
        return descriptor;
    };
    return fn;
};
class Test {
    static testDebounce(arg0) {
        console.log('防抖:', Date);
    }
}
__decorate([
    debounce(500),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Date]),
    __metadata("design:returntype", void 0)
], Test, "testDebounce", null);
window.addEventListener('resize', () => {
    Test.testDebounce(new Date());
});
